﻿using CleanLite.Domain.Interfaces;
using SqlSugar;

namespace CleanLite.Infrastructure
{
    public class BaseRepository<TEntity>(ISqlSugarClient db) : IBaseRepository<TEntity> where TEntity : class, new()
    {
        private readonly ISqlSugarClient db = db;

        public async Task<bool> DeleteAsync(object Id)
        {
            return await db.CopyNew().Deleteable<TEntity>(Id).ExecuteCommandAsync() > 0;
        }

        public async Task<List<TEntity>> GetListAsync()
        {
            return await db.CopyNew().Queryable<TEntity>().ToListAsync();
        }

        public async Task<List<TEntity>> GetToPageListAsync(int index, int size, int total)
        {
            return await db.Queryable<TEntity>().ToPageListAsync(index, size, total);
        }

        public async Task<bool> InsertAsync(TEntity entity)
        {
            return await db.CopyNew().Insertable(entity).ExecuteCommandAsync() > 0;
        }

        public async Task<bool> InsertAsync(List<TEntity> entitys)
        {
            return await db.CopyNew().Insertable(entitys).ExecuteCommandAsync() > 0;
        }

        public async Task<bool> UpdateAsync(TEntity entity)
        {
            return await db.CopyNew().Updateable(entity).ExecuteCommandAsync() > 0;
        }
    }
}